using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using SpaceShooter.StateManagement;
using Microsoft.Phone.Shell;
using SpaceShooter.Utilities;

namespace SpaceShooter
{
    public class SpaceShooter : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics { get; set; }
        public Match savedMatch;
        public SpriteBatch spriteBatch { get; set; }
        public SpriteFont spriteFont { get; set; }
        public ApplicationState State { get; set; }

        public SpaceShooter()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.SupportedOrientations = DisplayOrientation.Portrait;
            graphics.PreferredBackBufferHeight = 800;
            graphics.PreferredBackBufferWidth = 480;
            Content.RootDirectory = "Content";
            
            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            PhoneApplicationService appService = PhoneApplicationService.Current;
            appService.Launching += OnAppServiceLaunching;
            appService.Activated += OnAppServiceActivated;
            appService.Deactivated += OnAppServiceDeactivated;
            appService.Closing += OnAppServiceClosing;

            State = ApplicationState.MENU;
        }

        void OnAppServiceLaunching(object sender, LaunchingEventArgs e)
        {
            StateManagement.StateManager.launchMenu(this);
        }

        void OnAppServiceActivated(object sender, ActivatedEventArgs e)
        {
            savedMatch = SavedMatch.Load();
            if (savedMatch == null)
                StateManagement.StateManager.launchMenu(this);
            else
            {
                StateManagement.StateManager.launchGame(this, savedMatch);
                SavedMatch.Delete();
            }
        }

        void OnAppServiceDeactivated(object sender, DeactivatedEventArgs e)
        {
            if (State == ApplicationState.GAME)
            {
                GameState gs = (GameState)this.Services.GetService(typeof(GameState));
                if (gs != null)
                    SavedMatch.Save(gs);
            }
        }

        void OnAppServiceClosing(object sender, ClosingEventArgs e)
        {
            
        }

        protected override void Initialize()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            spriteFont = Content.Load<SpriteFont>("font");
            Services.AddService(typeof(GraphicsDeviceManager), graphics);
            Services.AddService(typeof(SpriteBatch), spriteBatch);
            Services.AddService(typeof(SpriteFont), spriteFont);

            base.Initialize();
        }

        protected override void LoadContent()
        {

        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                if (State == ApplicationState.GAME)
                {
                    GameState gs = (GameState)this.Services.GetService(typeof(GameState));
                    gs.pause = true;

                    Guide.BeginShowMessageBox("SAVE", "Do you want to save this match?", new List<string> { "Yes", "No" }, 0, MessageBoxIcon.Warning, asyncCallback =>
                    {
                        int? returned = Guide.EndShowMessageBox(asyncCallback);
                        if (returned == 0)
                        {
                            SavedMatch.Save(gs);
                            StateManager.launchMenu(this);
                        }
                        else
                            if (returned == 1)
                                StateManager.launchMenu(this);
                            else
                                gs.pause = false;
                            
                    }, null);
                }
                else
                    this.Exit();
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            base.Draw(gameTime);
        }
    }
}
